package com.talking.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.tabs.TabLayout;
import com.talking.App;
import com.talking.adapter.SearchAdapter;
import com.talking.bean.UserBean;
import com.talking.databinding.ActivitySearchBinding;
import com.talking.presenter.UserPresenter;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SearchActivity extends AppCompatActivity {
    private ActivitySearchBinding binding;
    private int nowTab=0;
    private List<UserBean> data=new ArrayList<>();
    private SearchAdapter adp;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivitySearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    private void initView() {
        binding.searchTab.addTab(binding.searchTab.newTab().setText("用户"));
//        binding.searchTab.addTab(binding.searchTab.newTab().setText("聊天记录"));
//        binding.searchTab.addTab(binding.searchTab.newTab().setText("占位1"));
//        binding.searchTab.addTab(binding.searchTab.newTab().setText("占位2"));
        binding.searchTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (Objects.requireNonNull(tab.getText()).toString().equals("用户")){
                    nowTab=0;
                    getList();
                }else if (Objects.requireNonNull(tab.getText()).toString().equals("聊天记录")){
                    nowTab=1;
                    getList();
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }
            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });
        setTitle("搜索:"+ App.query);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        adp=new SearchAdapter(this,data);
        binding.searchList.setAdapter(adp);
        binding.searchList.setOnItemClickListener((parent, view, position, id) -> {
            App.userBean =data.get(position);
            startActivity(new Intent(SearchActivity.this,PersonActivity.class));
        });
        getList();
    }
    private void getList() {
        data.clear();
        if (nowTab==0){
            UserPresenter.searchUser(0,10,App.query,new Callback(){
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    runOnUiThread(() -> {
                        try {
                            JSONArray ja = new JSONObject(response.body().string()).getJSONArray("data");
                            for (int i = 0; i < ja.length(); i++) {
                                JSONObject jo = ja.getJSONObject(i);
                                data.add(new UserBean(
                                        jo.getString("nickname"),
                                        jo.getString("email"),
                                        jo.getString("account"),
                                        jo.getString("iconUrl")
                                ));
                                adp.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    e.printStackTrace();
                }
            });
        }
        adp.notifyDataSetChanged();
    }
}
